package com.campus.backend.controller;

import com.campus.backend.bean.banner.BannerQueryParam;
import com.campus.backend.bean.banner.CreateBannerBean;
import com.campus.backend.bean.banner.UpdateBannerBean;
import com.campus.backend.service.BannerService;
import com.campus.backend.vo.banner.BannerVo;
import com.campus.common.bean.common.Pagination;
import com.campus.common.bean.common.Resp;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 轮播图表 前端控制器
 * </p>
 *
 * @author campus
 * @since 2024-01-01
 */
@RestController
@RequestMapping("/banner")
@Api(tags = "轮播图管理")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    /**
     * 分页查询轮播图列表
     *
     * @param param 查询参数
     * @return 分页结果
     */
    @GetMapping("/page")
    @ApiOperation("分页查询轮播图列表")
    public Resp<Pagination<BannerVo>> page(@Validated BannerQueryParam param) {
        PageInfo<BannerVo> pageInfo = bannerService.getBannerListPage(param);
        return Resp.successPageable(pageInfo);
    }

    /**
     * 根据ID获取轮播图详情
     *
     * @param bannerId 轮播图ID
     * @return 轮播图详情
     */
    @GetMapping("/detail/{bannerId}")
    @ApiOperation("获取轮播图详情")
    public Resp<BannerVo> detail(@PathVariable Integer bannerId) {
        BannerVo bannerVo = bannerService.getBannerById(bannerId);
        return Resp.success(bannerVo);
    }

    /**
     * 创建轮播图
     *
     * @param createBannerBean 轮播图信息
     * @return 轮播图ID
     */
    @PostMapping("/create")
    @ApiOperation("创建轮播图")
    public Resp<Integer> create(@RequestBody @Validated CreateBannerBean createBannerBean) {
        Integer bannerId = bannerService.createBanner(createBannerBean);
        return Resp.success(bannerId);
    }

    /**
     * 更新轮播图
     *
     * @param updateBannerBean 轮播图信息
     * @return 操作结果
     */
    @PostMapping("/update")
    @ApiOperation("更新轮播图")
    public Resp<String> update(@RequestBody @Validated UpdateBannerBean updateBannerBean) {
        bannerService.updateBanner(updateBannerBean);
        return Resp.success("更新成功");
    }

    /**
     * 删除轮播图
     *
     * @param bannerId 轮播图ID
     * @return 操作结果
     */
    @PostMapping("/delete/{bannerId}")
    @ApiOperation("删除轮播图")
    public Resp<String> delete(@PathVariable Integer bannerId) {
        bannerService.deleteBanner(bannerId);
        return Resp.success("删除成功");
    }

    /**
     * 批量删除轮播图
     *
     * @param bannerIds 轮播图ID列表
     * @return 操作结果
     */
    @PostMapping("/batchDelete")
    @ApiOperation("批量删除轮播图")
    public Resp<String> batchDelete(@RequestBody List<Integer> bannerIds) {
        bannerService.batchDeleteBanner(bannerIds);
        return Resp.success("批量删除成功");
    }

    /**
     * 更新轮播图状态
     *
     * @param bannerId 轮播图ID
     * @param status 状态
     * @return 操作结果
     */
    @PostMapping("/updateStatus/{bannerId}/{status}")
    @ApiOperation("更新轮播图状态")
    public Resp<String> updateStatus(@PathVariable Integer bannerId, @PathVariable Integer status) {
        bannerService.updateBannerStatus(bannerId, status);
        return Resp.success("状态更新成功");
    }
}
